package com.lt.flashcard.view;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.Init;

import com.lt.flashcard.Utility;
import com.lt.flashcard.model.entity.Cart;
import com.lt.flashcard.model.entity.Orderdetail;
import com.lt.flashcard.model.entity.Orderproduct;
import com.lt.flashcard.model.entity.User;
import com.lt.flashcard.model.service.AAService;
import com.lt.flashcard.model.service.EcommerceService;
import com.lt.flashcard.model.service.Repository;

public class OrderHistoryVm {

	@Init
	public void init() {
		AAService asrv = Repository.getInstance().getAAService();
		if(!asrv.isLogin()) {
			Utility.loginOrAccessDenied(false);
			return;
		}
	}
	
	public User getUser() {
		AAService aasrv = Repository.getInstance().getAAService();
		return aasrv.getCurrentUser();
	}
	
	/**
	 * Get the card
	 * @return
	 */
	public Cart getCart() {
		EcommerceService srv = Repository.getInstance().getEcommerceService();
		return srv.getCurrentCart();
	}
	
	public List<Orderdetail> getOrders() {
		EcommerceService srv = Repository.getInstance().getEcommerceService();
		return srv.getOrderHistory(getUser());
	}
	
	/**
	 * 
	 * @return
	 */
	public List<String> getProductsName(Orderdetail order) {
		List<String> result = new ArrayList<String>();
		Set<Orderproduct> set = order.getOrderproducts();
		for(Orderproduct item : set) {
			result.add(item.getProductname());
		}
		return result;
	}
	
	@Command
	public void search(@BindingParam("search") String search) {
		if(StringUtils.isNotBlank(search)) {
			String url = Utility.removeContextPath(Utility.generateUrl("store", null, "search=" + search));
			Utility.redirect(url, true);
		}
	}
}
